package com.wk.arth.od;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * ASDW 通过变换 数量一样多
 * 输出待更换的连续走位的最小可能长度
 * WASDAASD
 * 1 将第二个A替换为W 可得完美走位
 * WWWWAAAASSSS
 * 6
 * @author wukai
 * @date 2023/4/22 18:08
 * 最小覆盖字串问题
 */
public class Wan_mei_zou_wei {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(getResult(s));
    }

    private static int getResult(String str) {
        char[] chars = str.toCharArray();
        int avg = str.length()/4;
        if(str.length()%4 !=0){
            return 0;
        }
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            map.put(chars[i],map.getOrDefault(chars[i],0)+1);
        }

        int total = 0;
        for (Character character : map.keySet()) {
            int count = map.get(character);
            if(count>avg){
                // 多余字符数量
                map.put(character,count-avg);
                total+=count-avg;
            }else{
                map.put(character,0);
            }
        }
        int i=0;
        int j=0;
        int min = str.length()+1;
        while(j<str.length()){
            char jc = chars[j];
            if(map.get(jc)>0){
                total--;
            }
            map.put(jc,map.get(jc)-1);
            while(total == 0){
                min = Math.min(min,j-i+1);
                if(map.get(chars[i])>=0){
                    total++;
                }
                map.put(chars[i],map.get(chars[i])+1);
                i++;
            }
            j++;
        }
        return min>str.length()?0:min;
    }
}
